package com.hym.app;

import java.util.ArrayList;

import com.hym.hymlistview.DataEntity;
import com.hym.hymlistview.HymListview_mk;
import com.hym.hymlistview.HymListview_mk.IRefreshListener;
import com.hym.hymlistview.MKAdapter;
import com.hym.myrrefreshlistview.R;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;

public class MKActivity extends ActionBarActivity implements IRefreshListener {

	ArrayList<DataEntity> apk_list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mk);
		listview=(HymListview_mk)findViewById(R.id.listview);
		//设置接口绑定
		listview.setIRefreshListener(this);
		setData();
		showList(apk_list);
	}
	MKAdapter adapter;
	HymListview_mk listview;
	private void showList(ArrayList<DataEntity> apk_list) {
		if(adapter==null){
			adapter=new MKAdapter(this, apk_list);
			listview.setAdapter(adapter);
		}else{
			adapter.onDataChange(apk_list);
		}
	}
	private void setData() {
		apk_list = new ArrayList<DataEntity>();
		for (int i = 0; i < 10; i++) {
			DataEntity entity = new DataEntity();
			entity.setName("默认数据");
			entity.setDes("这是一个神奇的应用");
			entity.setInfo("50w用户");
			apk_list.add(entity);
		}
	}
	@Override
	public void onRefresh() {
		//实际场景，此处应该是异步获取网络数据，然后加载和改变相应的状态
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				//获取最新数据
				setReflashData();
				//通知界面显示
				showList(apk_list);
				//通知listview 刷新数据完毕；
				listview.refreshCompleted();
			}
		}, 2000);
		
	}
	/**
	 * 设置刷新数据
	 */
	private void setReflashData() {
		for (int i = 0; i < 2; i++) {
			DataEntity entity = new DataEntity();
			entity.setName("刷新数据");
			entity.setDes("这是一个神奇的应用");
			entity.setInfo("50w用户");
			apk_list.add(0,entity);
		}
	}
}
